package spring.cloud.tasks.executor.listener.report;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import spring.cloud.tasks.executor.listener.TreeCacheListener;
import spring.cloud.tasks.executor.utils.log.LogUtils;

@Slf4j
@Setter
@Getter
class ReportControlListener extends TreeCacheListener {
    private final ReportControlListenerHolder controlListener;

    public ReportControlListener(ReportControlListenerHolder controlListener) {
        this.controlListener = controlListener;
    }

    @Override
    protected void dataChanged(CuratorFramework curatorFramework, String path, TreeCacheEvent treeCacheEvent) {
        if (controlListener.isShutdown) {
            return;
        }
        if (ReportControlNode.isReportPath(controlListener.taskId, path) && (TreeCacheEvent.Type.NODE_ADDED == treeCacheEvent.getType() || TreeCacheEvent.Type.NODE_UPDATED == treeCacheEvent.getType())) {
            LogUtils.info(log, controlListener.taskId, "{} received report event from console, start to flush data to zk.", controlListener.taskId);
            controlListener.reportService.reportData2Zk();
        }
    }
}
